Page({
  data: {
    orderDetailResult:{

    },
    userId: null,
    orderId: null,
    members: [
      { name: 'Lee (我)', avatar: '/images/default-avatar.png' },
      { name: 'Sober', avatar: '/images/default-avatar.png' },
      { name: 'Tom', avatar: '/images/default-avatar.png' }
    ],
    loading: false,
    page: 1,
    scrollTop: 0,
    windowHeight: 0,
    isReachBottom: false,
    qrcodeUrl: null,
    showQrModal: false,
    codeUrl: null,
    showCode: false,
    showOrderButton: false,
    onumber: null,
  },
  handleShowCode(){
    this.setData({
      showQrModal: true
    })
  },
  onLoad(options) {
    wx.showLoading({
      title: '加载中',
    })
    setTimeout(()=>{
      wx.hideLoading()
    },1000)
    const orderId = options.orderId;
    // 保存订单ID
    this.setData({
      orderId: orderId
    });

    // 获取设备窗口高度
    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          windowHeight: res.windowHeight
        });
      }
    });
    // 先获取用户信息，然后再请求订单详情
    this.getUserInfoAndFetchOrder();
    
  },
  handleShowDialog() {
    wx.request({
      url: `http://localhost:9301/wx/generateCodeUrl?onumber=${this.data.orderDetailResult.order.onumber}`,
      method:"GET",
      success: (res)=>{
        this.setData({
          codeUrl : res.data.data
        })
      }
    })
    this.setData({
      showCode: true
    });
    // 设置一个定时器
    const timerId = setTimeout(() => {
      wx.request({
        url: `http://localhost:9301/wx/generateCodeUrl?onumber=${this.data.orderDetailResult.order.onumber}`,
        method:"GET",
        success: (res)=>{
          if(res.data.data == null){
            wx.showToast({
              title: '付款成功',
            })
            this.setData({
              showCode: false
            });
          }
        }
      })
    }, 10000);
  },
  hideQrModal() {
    this.setData({
      showQrModal: false
    });
  },
  handleCloseCode(){
    this.setData({
      showCode: false
    });
  },
  // 获取用户信息并请求订单详情
  getUserInfoAndFetchOrder() {
    wx.getStorage({
      key: "userInfo",
      success: (res) => {
        this.setData({
          userId: res.data.id
        }, () => {
          // 在 userId 设置完成后请求订单详情
          this.fetchOrderDetail();
        });
      },
      fail: (err) => {
        console.error('获取用户信息失败：', err);
        wx.showToast({
          title: '获取用户信息失败',
          icon: 'none'
        });
      }
    });
  },

  // 请求订单详情
  fetchOrderDetail() {
    if (!this.data.userId || !this.data.orderId) {
      console.error('userId 或 orderId 为空');
      return;
    }

    wx.request({
      url: `http://localhost:9301/order/findOrderById?orderId=${this.data.orderId}&userId=${this.data.userId}`,
      method:"GET",
      success: (res) => {
        this.setData({
          orderDetailResult: res.data.data,
          onumber: res.data.data.order.onumber
        })
        // 处理响应数据
        console.log('订单详情：', this.data.orderDetailResult);
        // TODO: 更新页面数据
        wx.request({
          url: `http://localhost:9301/wx/generateCodeUrl?onumber=${this.data.orderDetailResult.order.onumber}`,
          method:"GET",
          success: (res)=>{
            if(res.data.data == null){
              this.setData({
                showOrderButton: true
              })
            }
          }
        })
      },
      fail: (err) => {
        console.error('请求订单详情失败：', err);
        wx.showToast({
          title: '获取订单详情失败',
          icon: 'none'
        });
      }
    });
  },

  // ... 其他代码保持不变 ...
})